package com.s4j.struts.biz.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.Action;
import com.s4j.core.common.BaseAction;
import com.s4j.core.common.MyDateUtils;
import com.s4j.core.conset.Constant;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.manager.model.SysModule;
import com.s4j.core.manager.model.SysUser;
import com.s4j.struts.biz.model.BizCpAnswer;
import com.s4j.struts.biz.model.BizCpAppr;
import com.s4j.struts.biz.model.BizCpSupplement;
import com.s4j.struts.biz.model.PatientDoc;
import com.s4j.struts.biz.service.CokicpolypDocService;

@Controller("com.s4j.struts.biz.action.CokicpolypDocAction")
@Scope("prototype")
public class CokicpolypDocAction extends BaseAction {
	
	@Resource
	private CokicpolypDocService cokicpolypDocService;
	
	private PatientDoc patientDoc;
	
	private BizCpAppr cpAppr;
	
	private BizCpSupplement cpSupplement;
	
	private BizCpAnswer answer;

	public String mainQuery() {
		
		DatagridPaging datagridPaging = cokicpolypDocService.findUserByPaging(getPage(), getRows(), patientDoc);
		
		this.setDatagridPaging(datagridPaging);
		
		return Action.SUCCESS;
	}
	
	public String cokicpolypDoc_commonSave() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		SysUser login = (SysUser)session.getAttribute(Constant.BG_LOGIN_USER);
		if(patientDoc.getDocId() == null) {
			patientDoc.setDiseaseType(Constant.PATIENTDOC_DISEASETYPE_CP);
			patientDoc.setInputDate(MyDateUtils.getCurDateTime());
			patientDoc.setInputUserName(login.getUserName());
			cokicpolypDocService.saveCommon(patientDoc);
		} else {
			patientDoc.setDiseaseType(Constant.PATIENTDOC_DISEASETYPE_CP);
			cokicpolypDocService.updateCommon(patientDoc);
		}
		
		this.getResultMap().put("docId", patientDoc.getDocId());
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	@Override
	public void init(SysModule module, HttpServletRequest request) {
		
		if("docmgr_cokicpolypdocmgr_docEdit".equals(module.getModuleNo())) {
			String docId = request.getParameter("docId");
			request.setAttribute("docId", docId);
		}
		
		if("docmgr_cokicpolypdocmgr_common".equals(module.getModuleNo())) {
			String docId = request.getParameter("docId");
			if(docId != null && !"".equals(docId)) {
				request.setAttribute("patientDoc", cokicpolypDocService.getPatientDoc(new Long(docId)));
			}
		}
		
		if("docmgr_cokicpolypdocmgr_appr".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", cokicpolypDocService.getPatientDoc(docId));
			request.setAttribute("cpAppr", cokicpolypDocService.getCpApprByPatientId(docId));
		}
		
		if("docmgr_cokicpolypdocmgr_supplement".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", cokicpolypDocService.getPatientDoc(new Long(docId)));
			request.setAttribute("cpSupplement", cokicpolypDocService.getCpSupplementByPatientId(docId));
		}
		
		if("docmgr_cokicpolypdocmgr_answer".equals(module.getModuleNo())) {
			Long docId = new Long(request.getParameter("docId"));
			request.setAttribute("patientDoc", cokicpolypDocService.getPatientDoc(new Long(docId)));
			request.setAttribute("answer", cokicpolypDocService.getCpAnswerByPatientId(docId));
		}
	}
	
	public String cokicpolypDoc_apprSave1() throws Exception {
		if(cpAppr.getApprId() == null) {
			PatientDoc patient = cokicpolypDocService.getPatientDoc(patientDoc.getDocId());
			cpAppr.setPatient(patient);
			cokicpolypDocService.save(cpAppr);
		} else {
			BizCpAppr persis = cokicpolypDocService.get(BizCpAppr.class, cpAppr.getApprId());
			persis.setQ1(cpAppr.getQ1());
			persis.setQ5(cpAppr.getQ5());
			persis.setQ10(cpAppr.getQ10());
			persis.setQ10Value(cpAppr.getQ10Value());
			cokicpolypDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String cokicpolypDoc_apprSave2() throws Exception {
		if(cpAppr.getApprId() == null) {
			PatientDoc patient = cokicpolypDocService.getPatientDoc(patientDoc.getDocId());
			cpAppr.setPatient(patient);
			cokicpolypDocService.save(cpAppr);
		} else {
			BizCpAppr persis = cokicpolypDocService.get(BizCpAppr.class, cpAppr.getApprId());
			persis.setQ15(cpAppr.getQ15());
			persis.setQ20(cpAppr.getQ20());
			cokicpolypDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String cokicpolypDoc_apprSave3() throws Exception {
		if(cpAppr.getApprId() == null) {
			PatientDoc patient = cokicpolypDocService.getPatientDoc(patientDoc.getDocId());
			cpAppr.setPatient(patient);
			cokicpolypDocService.save(cpAppr);
		} else {
			BizCpAppr persis = cokicpolypDocService.get(BizCpAppr.class, cpAppr.getApprId());
			persis.setQ25_1(cpAppr.getQ25_1());
			persis.setQ25_2(cpAppr.getQ25_2());
			persis.setQ25_3(cpAppr.getQ25_3());
			persis.setQ25_4(cpAppr.getQ25_4());
			persis.setQ25_5(cpAppr.getQ25_5());
			persis.setQ25_6(cpAppr.getQ25_6());
			persis.setQ25_7(cpAppr.getQ25_7());
			persis.setQ25Value(cpAppr.getQ25Value());
			persis.setQ30_1(cpAppr.getQ30_1());
			persis.setQ30_2(cpAppr.getQ30_2());
			persis.setQ30_3(cpAppr.getQ30_3());
			persis.setQ30_4(cpAppr.getQ30_4());
			persis.setQ30_5(cpAppr.getQ30_5());
			persis.setQ30Value(cpAppr.getQ30Value());
			persis.setQ35_1(cpAppr.getQ35_1());
			persis.setQ35_2(cpAppr.getQ35_2());
			persis.setQ35_3(cpAppr.getQ35_3());
			persis.setQ35_4(cpAppr.getQ35_4());
			persis.setQ35_5(cpAppr.getQ35_5());
			persis.setQ35Value(cpAppr.getQ35Value());
			cokicpolypDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String cokicpolypDoc_supplementSave1() throws Exception {
		if(cpSupplement.getSuppId() == null) {
			PatientDoc patient = cokicpolypDocService.getPatientDoc(patientDoc.getDocId());
			cpSupplement.setPatient(patient);
			cokicpolypDocService.save(cpSupplement);
		} else {
			BizCpSupplement persis = cokicpolypDocService.get(BizCpSupplement.class, cpSupplement.getSuppId());
			persis.setQ1(cpSupplement.getQ1());
			persis.setQ5_1(cpSupplement.getQ5_1());
			persis.setQ5_2(cpSupplement.getQ5_2());
			persis.setQ5_3(cpSupplement.getQ5_3());
			persis.setQ5_4(cpSupplement.getQ5_4());
			persis.setQ5_5(cpSupplement.getQ5_5());
			persis.setQ5_6(cpSupplement.getQ5_6());
			persis.setQ10(cpSupplement.getQ10());
			persis.setQ15(cpSupplement.getQ15());
			persis.setQ20(cpSupplement.getQ20());
			persis.setQ25(cpSupplement.getQ25());
			persis.setQ30(cpSupplement.getQ30());
			cokicpolypDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String cokicpolypDoc_supplementSave2() throws Exception {
		if(cpSupplement.getSuppId() == null) {
			PatientDoc patient = cokicpolypDocService.getPatientDoc(patientDoc.getDocId());
			cpSupplement.setPatient(patient);
			cokicpolypDocService.save(cpSupplement);
		} else {
			BizCpSupplement persis = cokicpolypDocService.get(BizCpSupplement.class, cpSupplement.getSuppId());
			persis.setQ35(cpSupplement.getQ35());
			persis.setQ40(cpSupplement.getQ40());
			persis.setQ45(cpSupplement.getQ45());
			persis.setQ50(cpSupplement.getQ50());
			persis.setQ55(cpSupplement.getQ55());
			persis.setQ60(cpSupplement.getQ60());
			persis.setQ65(cpSupplement.getQ65());
			persis.setQ70(cpSupplement.getQ70());
			persis.setQ75(cpSupplement.getQ75());
			persis.setQ80(cpSupplement.getQ80());
			persis.setQ85(cpSupplement.getQ85());
			cokicpolypDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}
	
	public String cokicpolypDoc_answerSave1() throws Exception {
		if(answer.getId() == null) {
			PatientDoc patient = cokicpolypDocService.getPatientDoc(patientDoc.getDocId());
			answer.setPatient(patient);
			cokicpolypDocService.save(answer);
		} else {
			BizCpAnswer persis = cokicpolypDocService.get(BizCpAnswer.class, answer.getId());
			persis.setQ1(answer.getQ1());
			persis.setQ2(answer.getQ2());
			persis.setQ3(answer.getQ3());
			persis.setQ4(answer.getQ4());
			persis.setQ5(answer.getQ5());
			persis.setQ6(answer.getQ6());
			persis.setQ7(answer.getQ7());
			persis.setQ8_1(answer.getQ8_1());
			persis.setQ8_2(answer.getQ8_2());
			persis.setQ8_3(answer.getQ8_3());
			persis.setQ8_4(answer.getQ8_4());
			persis.setQ8_5(answer.getQ8_5());
			persis.setQ9(answer.getQ9());
			persis.setQ10(answer.getQ10());
			persis.setQ11(answer.getQ11());
			persis.setQ12(answer.getQ12());
			persis.setQ13(answer.getQ13());
			persis.setQ14(answer.getQ14());
			persis.setQ15(answer.getQ15());
			persis.setQ16(answer.getQ16());
			persis.setQ17(answer.getQ17());
			persis.setQ18(answer.getQ18());
			persis.setQ19_1(answer.getQ19_1());
			persis.setQ19_2(answer.getQ19_2());
			persis.setQ19_3(answer.getQ19_3());
			persis.setQ19_4(answer.getQ19_4());
			persis.setQ19_5(answer.getQ19_5());
			persis.setQ19_6(answer.getQ19_6());
			persis.setQ19_7(answer.getQ19_7());
			persis.setQ20(answer.getQ20());
			persis.setQ21_1(answer.getQ21_1());
			persis.setQ21_2(answer.getQ21_2());
			persis.setQ21_3(answer.getQ21_3());
			persis.setQ21_4(answer.getQ21_4());
			persis.setQ21_5(answer.getQ21_5());
			persis.setQ21_6(answer.getQ21_6());
			persis.setQ21_7(answer.getQ21_7());
			persis.setQ22(answer.getQ22());
			persis.setQ23(answer.getQ23());
			persis.setQ24(answer.getQ24());
			persis.setQ25(answer.getQ25());
			persis.setQ26(answer.getQ26());
			persis.setQ27(answer.getQ27());
			persis.setQ28_1(answer.getQ28_1());
			persis.setQ28_2(answer.getQ28_2());
			persis.setQ28_3(answer.getQ28_3());
			persis.setQ28_4(answer.getQ28_4());
			persis.setQ28_5(answer.getQ28_5());
			persis.setQ29(answer.getQ29());
			persis.setQ30(answer.getQ30());
			persis.setQ31(answer.getQ31());
			persis.setQ32_1(answer.getQ32_1());
			persis.setQ32_2(answer.getQ32_2());
			persis.setQ32_3(answer.getQ32_3());
			persis.setQ32_4(answer.getQ32_4());
			persis.setQ32_5(answer.getQ32_5());
			persis.setQ33_1(answer.getQ33_1());
			persis.setQ33_2(answer.getQ33_2());
			persis.setQ33_3(answer.getQ33_3());
			persis.setQ33_4(answer.getQ33_4());
			persis.setQ33_5(answer.getQ33_5());
			persis.setQ33_6(answer.getQ33_6());
			persis.setQ33_7(answer.getQ33_7());
			persis.setQ33Value(answer.getQ33Value());
			persis.setQ34(answer.getQ34());
			persis.setQ35(answer.getQ35());
			persis.setQ35Value(answer.getQ35Value());
			persis.setQ36(answer.getQ36());
			persis.setQ37(answer.getQ37());
			persis.setQ37Value(answer.getQ37Value());
			persis.setQ38(answer.getQ38());
			persis.setQ39(answer.getQ39());
			persis.setQ39Value(answer.getQ39Value());
			persis.setQ40(answer.getQ40());
			persis.setQ41(answer.getQ41());
			persis.setQ41Value(answer.getQ41Value());
			persis.setQ42(answer.getQ42());
			persis.setQ43(answer.getQ43());
			persis.setQ43Value(answer.getQ43Value());
			persis.setQ44(answer.getQ44());
			persis.setQ45(answer.getQ45());
			persis.setQ45Value(answer.getQ45Value());
			persis.setQ46(answer.getQ46());
			persis.setQ47(answer.getQ47());
			persis.setQ47Value(answer.getQ47Value());
			persis.setQ48(answer.getQ48());
			persis.setQ49(answer.getQ49());
			persis.setQ49Value(answer.getQ49Value());
			persis.setQ50(answer.getQ50());
			persis.setQ51(answer.getQ51());
			persis.setQ51Value(answer.getQ51Value());
			persis.setQ52(answer.getQ52());
			persis.setQ53(answer.getQ53());
			persis.setQ54(answer.getQ54());
			persis.setQ55(answer.getQ55());
			persis.setQ56_1(answer.getQ56_1());
			persis.setQ56_2(answer.getQ56_2());
			persis.setQ56_3(answer.getQ56_3());
			persis.setQ56_4(answer.getQ56_4());
			persis.setQ56_5(answer.getQ56_5());
			persis.setQ56_6(answer.getQ56_6());
			persis.setQ56_7(answer.getQ56_7());
			persis.setQ56_8(answer.getQ56_8());
			persis.setQ58(answer.getQ58());
			persis.setQ59(answer.getQ59());
			persis.setQ60(answer.getQ60());
			persis.setQ61(answer.getQ61());
			persis.setQ62(answer.getQ62());
			persis.setQ63(answer.getQ63());
			persis.setQ64(answer.getQ64());
			persis.setQ65(answer.getQ65());
			persis.setQ66(answer.getQ66());
			persis.setQ67(answer.getQ67());
			persis.setQ68(answer.getQ68());
			persis.setQ69(answer.getQ69());
			persis.setQ70(answer.getQ70());
			persis.setQ71(answer.getQ71());
			persis.setQ72(answer.getQ72());
			persis.setQ73(answer.getQ73());
			persis.setQ74(answer.getQ74());
			persis.setQ75(answer.getQ75());
			persis.setQ76(answer.getQ76());
			persis.setQ77(answer.getQ77());
			persis.setQ78(answer.getQ78());
			persis.setQ79(answer.getQ79());
			persis.setQ80(answer.getQ80());
			persis.setQ81(answer.getQ81());
			persis.setQ82(answer.getQ82());
			persis.setQ83(answer.getQ83());
			persis.setQ85(answer.getQ85());
			persis.setQ86(answer.getQ86());
			persis.setQ87(answer.getQ87());
			persis.setQ88(answer.getQ88());
			persis.setQ89(answer.getQ89());
			persis.setQ90(answer.getQ90());
			persis.setQ91(answer.getQ91());
			persis.setQ92(answer.getQ92());
			persis.setQ93(answer.getQ93());
			persis.setQ94(answer.getQ94());
			persis.setQ95(answer.getQ95());
			cokicpolypDocService.update(persis);
		}
		this.getResultMap().put("rtnCode", Action.SUCCESS);
		this.getResultMap().put("rtnMsg", "保存成功");
		
		return Action.SUCCESS;
	}

	public PatientDoc getPatientDoc() {
		return patientDoc;
	}

	public void setPatientDoc(PatientDoc patientDoc) {
		this.patientDoc = patientDoc;
	}

	public BizCpAppr getCpAppr() {
		return cpAppr;
	}

	public void setCpAppr(BizCpAppr cpAppr) {
		this.cpAppr = cpAppr;
	}

	public BizCpSupplement getCpSupplement() {
		return cpSupplement;
	}

	public void setCpSupplement(BizCpSupplement cpSupplement) {
		this.cpSupplement = cpSupplement;
	}

	public BizCpAnswer getAnswer() {
		return answer;
	}

	public void setAnswer(BizCpAnswer answer) {
		this.answer = answer;
	}
}
